﻿cmake_minimum_required(VERSION 2.8)
project(avdevice)
file(GLOB lib_hdrs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
file(GLOB lib_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.def)
list(APPEND lib_srcs
 alldevices.c 
 avdevice.c 
 utils.c)

if(${HAVE_LIBC_MSVCRT})
  list(APPEND lib_srcs file_open.c)
endif()
if(${CONFIG_SHARED})
  list(APPEND lib_srcs reverse.c)
endif()

# input/output devices
if(${CONFIG_ALSA_INDEV})
  list(APPEND lib_srcs alsa_dec.c alsa.c timefilter.c)
endif()
if(${CONFIG_ALSA_OUTDEV})
  list(APPEND lib_srcs alsa_enc.c alsa.c)
endif()
if(${CONFIG_ANDROID_CAMERA_INDEV})
  list(APPEND lib_srcs android_camera.c)
endif()
if(${CONFIG_AUDIOTOOLBOX_OUTDEV})
  list(APPEND lib_srcs audiotoolbox.c)
endif()
if(${CONFIG_AVFOUNDATION_INDEV})
  list(APPEND lib_srcs avfoundation.c)
endif()
if(${CONFIG_BKTR_INDEV})
  list(APPEND lib_srcs bktr.c)
endif()
if(${CONFIG_CACA_OUTDEV})
  list(APPEND lib_srcs caca.c)
endif()
if(${CONFIG_DECKLINK_OUTDEV})
  list(APPEND lib_srcs decklink_enc.c decklink_enc_c.c decklink_common.c)
endif()
if(${CONFIG_DECKLINK_INDEV})
  list(APPEND lib_srcs decklink_dec.c decklink_dec_c.c decklink_common.c)
endif()
if(${CONFIG_DSHOW_INDEV})
  list(APPEND lib_srcs dshow_crossbar.c dshow.c dshow_enummediatypes.c 
 dshow_enumpins.c dshow_filter.c 
 dshow_pin.c dshow_common.c)
endif()
if(${CONFIG_FBDEV_INDEV})
  list(APPEND lib_srcs fbdev_dec.c 
 fbdev_common.c)
endif()
if(${CONFIG_FBDEV_OUTDEV})
  list(APPEND lib_srcs fbdev_enc.c 
 fbdev_common.c)
endif()
if(${CONFIG_GDIGRAB_INDEV})
  list(APPEND lib_srcs gdigrab.c)
endif()
if(${CONFIG_IEC61883_INDEV})
  list(APPEND lib_srcs iec61883.c)
endif()
if(${CONFIG_JACK_INDEV})
  list(APPEND lib_srcs jack.c timefilter.c)
endif()
if(${CONFIG_KMSGRAB_INDEV})
  list(APPEND lib_srcs kmsgrab.c)
endif()
if(${CONFIG_LAVFI_INDEV})
  list(APPEND lib_srcs lavfi.c)
endif()
if(${CONFIG_OPENAL_INDEV})
  list(APPEND lib_srcs openal-dec.c)
endif()
if(${CONFIG_OPENGL_OUTDEV})
  list(APPEND lib_srcs opengl_enc.c)
endif()
if(${CONFIG_OSS_INDEV})
  list(APPEND lib_srcs oss_dec.c oss.c)
endif()
if(${CONFIG_OSS_OUTDEV})
  list(APPEND lib_srcs oss_enc.c oss.c)
endif()
if(${CONFIG_PULSE_INDEV})
  list(APPEND lib_srcs pulse_audio_dec.c 
 pulse_audio_common.c timefilter.c)
endif()
if(${CONFIG_PULSE_OUTDEV})
  list(APPEND lib_srcs pulse_audio_enc.c 
 pulse_audio_common.c)
endif()
if(${CONFIG_SDL2_OUTDEV})
  list(APPEND lib_srcs sdl2.c)
endif()
if(${CONFIG_SNDIO_INDEV})
  list(APPEND lib_srcs sndio_dec.c sndio.c)
endif()
if(${CONFIG_SNDIO_OUTDEV})
  list(APPEND lib_srcs sndio_enc.c sndio.c)
endif()
if(${CONFIG_V4L2_INDEV})
  list(APPEND lib_srcs v4l2.c v4l2-common.c timefilter.c)
endif()
if(${CONFIG_V4L2_OUTDEV})
  list(APPEND lib_srcs v4l2enc.c v4l2-common.c)
endif()
if(${CONFIG_VFWCAP_INDEV})
  list(APPEND lib_srcs vfwcap.c)
endif()
if(${CONFIG_XCBGRAB_INDEV})
  list(APPEND lib_srcs xcbgrab.c)
endif()
if(${CONFIG_XV_OUTDEV})
  list(APPEND lib_srcs xv.c)
endif()

# external libraries
if(${CONFIG_LIBCDIO_INDEV})
  list(APPEND lib_srcs libcdio.c)
endif()
if(${CONFIG_LIBDC1394_INDEV})
  list(APPEND lib_srcs libdc1394.c)
endif()

# Windows resource file
if(${HAVE_GNU_WINDRES})
  list(APPEND lib_srcs avdeviceres.c)
endif()

set(CMAKE_DEBUG_POSTFIX "D")

macro(cl_asm)
  foreach(_var ${ARGN})
    list(APPEND lib_objs ${_var}.obj)
	file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${_var} src)
	file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR}/${_var}.obj dst)
    add_custom_command(OUTPUT ${dst}
	  COMMAND cd ARGS ${CMAKE_CURRENT_SOURCE_DIR}/..
	  COMMAND yasm ARGS -I. -P config.asm -f win64 ${src} -o ${dst} MAIN_DEPENDENCY ${_var})
  endforeach()
endmacro()
cl_asm(${lib_asms})

include_directories(.. ../compat ../compat/atomics/win32 x86)
add_definitions(-DHAVE_AV_CONFIG_H)
add_library(${PROJECT_NAME} SHARED ${lib_srcs} ${lib_asms} ${lib_objs})
target_link_libraries(${PROJECT_NAME} ${lib_libs} avutil avformat avcodec swresample swscale avfilter vfw32 shlwapi)
install(FILES ${lib_hdrs} DESTINATION include/ffmpeg/lib${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets RUNTIME DESTINATION bin ARCHIVE DESTINATION lib)
install(EXPORT ${PROJECT_NAME}-targets DESTINATION lib/cmake FILE ${PROJECT_NAME}-config.cmake)

if(BUILD_STATIC)
  add_library(${PROJECT_NAME}S STATIC ${lib_srcs} ${lib_asms} ${lib_objs})
  install(TARGETS ${PROJECT_NAME}S EXPORT ${PROJECT_NAME}S-targets RUNTIME DESTINATION bin ARCHIVE DESTINATION lib)
  install(EXPORT ${PROJECT_NAME}S-targets DESTINATION lib/cmake FILE ${PROJECT_NAME}S-config.cmake)
endif()

